home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / xnot12a.zip / WORD.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  7KB  |  325 lines

  1. #include "jam.h"
  2. /*
  3.  *        Word mode commands.
  4.  * The routines in this file
  5.  * implement commands that work word at
  6.  * a time. There are all sorts of word mode
  7.  * commands.
  8.  */
  9. #include    "def.h"
  10.  
  11. /*
  12.  * Move the cursor backward by
  13.  * "n" words. All of the details of motion
  14.  * are performed by the "backchar" and "forwchar"
  15.  * routines.
  16.  */
  17. /*ARGSUSED*/
  18. backword(f, n)
  19. int f, n;
  20. {
  21.     if (n < 0) 
  22.       return forwword(f | FFRAND, -n);
  23.     if (backchar(FFRAND, 1) == FALSE)
  24.         return FALSE;
  25.     while (n--) {
  26.         while (inword() == FALSE) {
  27.             if (backchar(FFRAND, 1) == FALSE)
  28.                 return TRUE;
  29.         }
  30.         while (inword() != FALSE) {
  31.             if (backchar(FFRAND, 1) == FALSE)
  32.                 return TRUE;
  33.         }
  34.     }
  35.     return forwchar(FFRAND, 1);
  36. }
  37.  
  38. /*
  39.  * Move the cursor forward by
  40.  * the specified number of words. All of the
  41.  * motion is done by "forwchar".
  42.  */
  43. /*ARGSUSED*/
  44. forwword(f, n)
  45. int f, n;
  46. {
  47.   if (n < 0)
  48.     return backword (f | FFRAND, -n);
  49.   while (n--) {
  50.     while (inword() == FALSE) {
  51.       if (forwchar(FFRAND, 1) == FALSE)
  52.         return TRUE;
  53.     }
  54.     while (inword() != FALSE) {
  55.       if (forwchar(FFRAND, 1) == FALSE)
  56.         return TRUE;
  57.     }
  58.  
  59.     /* get to first letter in the new word (JAM)
  60.     */
  61.     while (inword() == FALSE) {
  62.       if (forwchar(FFRAND, 1) == FALSE)
  63.         return TRUE;
  64.       }
  65.   }
  66.   return TRUE;
  67. }
  68.  
  69. /*
  70.  * Move the cursor forward by
  71.  * the specified number of words. As you move,
  72.  * convert any characters to upper case.
  73.  */
  74. /*ARGSUSED*/
  75. upperword(f, n)
  76. int f, n;
  77. {
  78.  
  79.   register int    c;
  80.  
  81.   if (curbp->b_flag & BFVIEW)
  82.     {
  83.       ttbeep();
  84.       return FALSE;
  85.     }
  86.   if (filetimechanged(curbp))
  87.     return(FALSE);
  88.   clearUndo(curbp);
  89.   cacheUpper((RSIZE)n);
  90.  
  91.     if (n < 0) 
  92.       return FALSE;
  93.     while (n--) {
  94.         while (inword() == FALSE) 
  95.           {
  96.             if (forwchar(FFRAND, 1) == FALSE)
  97.                 return TRUE;
  98.           }
  99.         while (inword() != FALSE) {
  100.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  101.             if (ISLOWER(c) != FALSE) 
  102.               {
  103.               c = TOUPPER(c);
  104.               lchange(WFHARD);
  105.                           changelineflag(curwp->w_dotp, TRUE);
  106.               lputc(curwp->w_dotp, curwp->w_doto, (char)c);
  107.               }
  108.             if (forwchar(FFRAND, 1) == FALSE)
  109.                 return TRUE;
  110.         }
  111.     }
  112.     return TRUE;
  113. }
  114.  
  115. /*
  116.  * Move the cursor forward by
  117.  * the specified number of words. As you move
  118.  * convert characters to lower case.
  119.  */
  120. /*ARGSUSED*/
  121. lowerword(f, n)
  122. int f, n;
  123. {
  124.     register int    c;
  125.     int first = TRUE;
  126.  
  127.   if (curbp->b_flag & BFVIEW)
  128.     {
  129.       ttbeep();
  130.       return FALSE;
  131.     }
  132.   if (filetimechanged(curbp))
  133.     return(FALSE);
  134.   clearUndo(curbp);
  135.   cacheLower((RSIZE)n);
  136.  
  137.     if (n < 0) return FALSE;
  138.     while (n--) {
  139.         while (inword() == FALSE) {
  140.             if (forwchar(FFRAND, 1) == FALSE)
  141.                 return TRUE;
  142.         }
  143.         while (inword() != FALSE) {
  144.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  145.             if (ISUPPER(c) != FALSE) {
  146.                 c = TOLOWER(c);
  147.                 lchange(WFHARD);
  148.                                 changelineflag(curwp->w_dotp, TRUE);
  149.                 lputc(curwp->w_dotp, curwp->w_doto, (char)c);
  150.             }
  151.             if (forwchar(FFRAND, 1) == FALSE)
  152.                 return TRUE;
  153.         }
  154.     }
  155.     return TRUE;
  156. }
  157.  
  158. /*
  159.  * Move the cursor forward by
  160.  * the specified number of words. As you move
  161.  * convert the first character of the word to upper
  162.  * case, and subsequent characters to lower case. Error
  163.  * if you try and move past the end of the buffer.
  164.  */
  165. /*ARGSUSED*/
  166. capword(f, n)
  167. int f, n;
  168. {
  169.   register int    c;
  170.  
  171.   if (curbp->b_flag & BFVIEW)
  172.     {
  173.       ttbeep();
  174.       return FALSE;
  175.     }
  176.   if (filetimechanged(curbp))
  177.     return(FALSE);
  178.   clearUndo(curbp);
  179.     if (n < 0) return FALSE;
  180.     while (n--) {
  181.         while (inword() == FALSE) {
  182.             if (forwchar(FFRAND, 1) == FALSE)
  183.                 return TRUE;
  184.         }
  185.         if (inword() != FALSE) {
  186.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  187.             if (ISLOWER(c) != FALSE) {
  188.                 c = TOUPPER(c);
  189.                 lchange(WFHARD);
  190.                                 curwp->w_dotp->l_flag |= LFCHANGE;
  191.                 lputc(curwp->w_dotp, curwp->w_doto, (char)c);
  192.             }
  193.             if (forwchar(FFRAND, 1) == FALSE)
  194.                 return TRUE;
  195.             while (inword() != FALSE) {
  196.                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  197.                 if (ISUPPER(c) != FALSE) {
  198.                     c = TOLOWER(c);
  199.                     lchange(WFHARD);
  200.                                         curwp->w_dotp->l_flag |= LFCHANGE;
  201.                     lputc(curwp->w_dotp, curwp->w_doto, 
  202.                         (char)c);
  203.                 }
  204.                 if (forwchar(FFRAND, 1) == FALSE)
  205.                     return TRUE;
  206.             }
  207.         }
  208.     }
  209.     return TRUE;
  210. }
  211.  
  212. /*
  213.  * Kill forward by "n" words.
  214.  */
  215. /*ARGSUSED*/
  216. delfword(f, n)
  217. int f, n;
  218. {
  219.     register RSIZE    size;
  220.     register LINE    *dotp;
  221.     register int    doto;
  222.  
  223.   if (curbp->b_flag & BFVIEW)
  224.     {
  225.       ttbeep();
  226.       return FALSE;
  227.     }
  228.   if (filetimechanged(curbp))
  229.     return(FALSE);
  230.   clearUndo(curbp);
  231.  
  232.     if (n < 0)
  233.         return FALSE;
  234.     if ((lastflag&CFKILL) == 0)        /* Purge kill buffer.    */
  235.         kdelete();
  236.     thisflag |= CFKILL;
  237.     dotp = curwp->w_dotp;
  238.     doto = curwp->w_doto;
  239.     size = 0;
  240.     while (n--) {
  241.         while (inword() == FALSE) {
  242.             if (forwchar(FFRAND, 1) == FALSE)
  243.                 goto out;    /* Hit end of buffer.    */
  244.             ++size;
  245.         }
  246.         while (inword() != FALSE) {
  247.             if (forwchar(FFRAND, 1) == FALSE)
  248.                 goto out;    /* Hit end of buffer.    */
  249.             ++size;
  250.         }
  251.     }
  252. out:
  253.     curwp->w_dotp = dotp;
  254.     curwp->w_doto = doto;
  255.     return (ldelete(size, KFORW));
  256. }
  257.  
  258. /*
  259.  * Kill backwards by "n" words. The rules
  260.  * for success and failure are now different, to prevent
  261.  * strange behavior at the start of the buffer. The command
  262.  * only fails if something goes wrong with the actual delete
  263.  * of the characters. It is successful even if no characters
  264.  * are deleted, or if you say delete 5 words, and there are
  265.  * only 4 words left. I considered making the first call
  266.  * to "backchar" special, but decided that that would just
  267.  * be wierd. Normally this is bound to "M-Rubout" and
  268.  * to "M-Backspace".
  269.  */
  270. /*ARGSUSED*/
  271. delbword(f, n)
  272. int f, n;
  273. {
  274.     register RSIZE    size;
  275.     VOID        kdelete();
  276.  
  277.   if (curbp->b_flag & BFVIEW)
  278.     {
  279.       ttbeep();
  280.       return FALSE;
  281.     }
  282.   if (filetimechanged(curbp))
  283.     return(FALSE);
  284.   clearUndo(curbp);
  285.  
  286.     if (n < 0) return FALSE;
  287.     if ((lastflag&CFKILL) == 0)        /* Purge kill buffer.    */
  288.         kdelete();
  289.     thisflag |= CFKILL;
  290.     if (backchar(FFRAND, 1) == FALSE)
  291.         return (TRUE);            /* Hit buffer start.    */
  292.     size = 1;                /* One deleted.        */
  293.     while (n--) {
  294.         while (inword() == FALSE) {
  295.             if (backchar(FFRAND, 1) == FALSE)
  296.                 goto out;    /* Hit buffer start.    */
  297.             ++size;
  298.         }
  299.         while (inword() != FALSE) {
  300.             if (backchar(FFRAND, 1) == FALSE)
  301.                 goto out;    /* Hit buffer start.    */
  302.             ++size;
  303.         }
  304.     }
  305.     if (forwchar(FFRAND, 1) == FALSE)
  306.         return FALSE;
  307.     --size;                    /* Undo assumed delete. */
  308. out:
  309.     return ldelete(size, KBACK);
  310. }
  311.  
  312. /*
  313.  * Return TRUE if the character at dot
  314.  * is a character that is considered to be
  315.  * part of a word. The word character list is hard
  316.  * coded. Should be setable.
  317.  */
  318. inword() 
  319. {
  320. /* can't use lgetc in ISWORD due to bug in OSK cpp 
  321. */
  322.   return (curwp->w_doto != llength(curwp->w_dotp) && 
  323.       ISWORD(curwp->w_dotp->l_text[curwp->w_doto]));
  324. }
  325.